<?php

namespace common\repositories\outstock;

use common\entities\outstock\MyscrmDeliveryProductEntity;
use common\repositories\BaseRepository;
use common\entities\ContractEntity;

/**
 * Created by PhpStorm.
 * User: yuyj
 * Date: 16-12-12
 * Time: 上午11:00
 */
class DeliveryProductRepository extends BaseRepository
{
    public static $strToSignTypeMap=['新签'=>1,'续签'=>3,'增购'=>2];
    public function addProduct($data){
        $entity=new MyscrmDeliveryProductEntity();
        $entity->load($data,'');
        $saveRes=$entity->save();
        if($saveRes===false){
            return self::setAndReturn(__LINE__,$entity->getAllFirstErrorMessage());
        }
        return $saveRes;
    }
    public function findProductsBySubOrderId($subOrderId){
        return MyscrmDeliveryProductEntity::find()->where(['sub_order_id' => $subOrderId])->asArray(true)->all();
    }
    public function getApplicationIdByAppInfoId($applicationInfoId){
        $sql = "SELECT `authorize_application_id` FROM `myscrm_out_stock_allow_product` WHERE `apply_application_id`=:applicationInfoId";
        return ContractEntity::getDb()->createCommand($sql,[':applicationInfoId'=>$applicationInfoId])->queryOne();
    }


    public static function getDeliveryInfoByCAID($contract_application_id){
        return MyscrmDeliveryProductEntity::find()
            ->select('myscrm_delivery_product.delivery_guid,order_code as delivery_code')
            ->leftJoin('myscrm_delivery_order','myscrm_delivery_order.delivery_guid = myscrm_delivery_product.delivery_guid')
            ->where(['contract_application_id' => $contract_application_id])->asArray(true)->all();
    }
}